package com.service;

import Dbcon.DBcon;
import com.common.pcBean;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ztc on 16-3-17.上午10:21
 */
public class pcFixService {
    //pcFix(xh varchar(15),name varchar(10),phone varchar(12),qq varchar(15),weixin varchar(30),email varchar(30),describ varchar(100),imgPath varchar(30))
    public boolean insertPc(pcBean pc){
        String sql="insert into pcFix values(?,?,?,?,?,?,?,?);";
        Connection connection= DBcon.getConnction();
        PreparedStatement pr=null;
        try {
            pr=connection.prepareStatement(sql);
            pr.setString(1,pc.getXh());
            pr.setString(2,pc.getName());
            pr.setString(3,pc.getPhone());
            pr.setString(4,pc.getQq());
            pr.setString(5,pc.getWeixin());
            pr.setString(6,pc.getEmail());
            pr.setString(7,pc.getDescrib());
            pr.setString(8,pc.getImgPath());
            int i=pr.executeUpdate();
            if(i==1)
                return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    public int recordCount(){
        String sql="select count(*) from pcFix;";
        Connection connection=DBcon.getConnction();
        try {
            Statement sr=connection.createStatement();
            ResultSet re=sr.executeQuery(sql);
            re.next();
            return re.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }
    public List<pcBean> getLimit(int arg1,int arg2){
        List<pcBean> list=new ArrayList<pcBean>();
        String sql="select*from pcFix limit ?,?;";
        Connection connection=DBcon.getConnction();
        PreparedStatement pr=null;
        try {
            pr=connection.prepareStatement(sql);
            pr.setInt(1,arg1);
            pr.setInt(2,arg2);
            ResultSet re=pr.executeQuery();
            while(re.next()){
                list.add(new pcBean(re.getString("xh"),re.getString("name"),
                        re.getString("phone"),re.getString("qq"),
                        re.getString("weixin"),re.getString("email"),
                        re.getString("describ"),re.getString("imgPath")));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public static void main(String[] args){
        List<pcBean> list=new pcFixService().getLimit(0, 2);
        for(pcBean p:list){
            System.out.println(p.getXh());
        }
    }
}
